@extends('front.layouts.app')

@section('content')
	<section id="cart_items">
		<div class="container">
			@guest() 
				@if(!session('clear-alert-no-login'))
					<div class="alert alert-warning" role="alert" style="transition: all .4s"> 
						<button type="button" class="close" data-dismiss="alert" aria-label="Close">
						    <span aria-hidden="true" onclick="clearboth()">&times;</span>
						  </button>
						<h4 class="alert-heading">默认保存2个小时，请及时下单!</h4>
						<p>
							登录之后永久保存.
						</p>
	  				</div>
				@endif
			@endguest	

				<nav aria-label="breadcrumb" role="navigation">
				  <ol class="breadcrumb">
				    <li class="breadcrumb-item " aria-current="page">Home</li>
				    <li class="breadcrumb-item active" aria-current="page">购物车</li>
				  </ol>
				</nav>		
			@if(!$carts->isEmpty())
			<div class="table-responsive cart_info">
				<table class="table table-condensed">
					<thead>
						<tr class="cart_menu">
							<td class="image" >Item</td>
							<td class="description"  >商品名字</td>
							<td class="price">Price</td>
							<td class="quantity">Quantity</td>
							<td class="total">Total</td>
							<td></td>
						</tr>
					</thead>
					<tbody>
						 @guest
						   @include('front.cart.notlogin',['carts'=>$carts])
						 @else
						   @foreach($carts as $cart)

						<tr>
							<td class="cart_product"  > 
								<a href="{{route('details',$cart->id)}}"><img src="{{asset('productpics').'/'.$cart->pimg}}" width="90" height="90" style="text-align: center;margin:0 auto;" alt=""></a>
							</td>
							<td class="cart_description">
								<h4><a href="">{{$cart->name}}</a></h4>
								<p>商品编号:{{$cart->id}}</p>
								<p id="stock">库存:{{$cart->stock}}</p>
							</td>
							<td class="cart_price" >
								<span style="float:left">￥</span><p id="price-{{$cart->id}}"> {{$cart->price }}</p>
							</td>
							<td class="cart_quantity">
								<div class="cart_quantity_button">
									<a class="cart_quantity_up" href="javascript:;"  onclick="update_count({{$cart->id}},'{{$cart->rowId}}','0','',{{$cart->stock}})"> + </a>
									<input class="cart_quantity_input" id="quantity-{{$cart->id}}"  type="text" name="quantity" value="{{$cart->qty}}" onchange="update_count({{$cart->id}},'{{$cart->rowId}}',2,1,{{$cart->stock}})" autocomplete="off" size="2">
									<a class="cart_quantity_down" href="javascript:;" onclick="update_count({{$cart->id}},'{{$cart->rowId}}','1','',{{$cart->stock}})"> - </a>
								</div>
							</td>
							<td class="cart_total">
								 <p class="cart_total_price" id="tl-{{$cart->id}}">￥ {{$cart->price * $cart->qty}}</p>
							</td>
							<td class="cart_delete">
								<a class="cart_quantity_delete" href="{{ route('cart-del',$cart->rowId) }}"><i class="fa fa-times"></i></a>
							</td>
						</tr>
			 @endforeach
						 @endif 
					</tbody>
				</table>

			</div>
			 @else
			 	<h1 style="text-align: center;">还不快去剁手?(空荡荡的感觉真不好呢)</h1><br />
			 	<small class="text-danger">* 默认保存2个小时，请及时下单!</small>
			 @endif
		</div>
	</section> <!--/#cart_items-->
	

	<section id="do_action">
		<div class="container">
			<div class="heading">
				<h3>下一步要做什么 ?</h3>
				<p>如果你选择了一个折扣代码或积分奖励你想使用或想估计一下费用。</p>
			</div>
			<div class="row">
				<div class="col-sm-6">
					<div class="chose_area">
						<ul class="user_option">
							<li>
								<input type="checkbox">
								<label>使用优惠券代码</label>
							</li> 
						</ul>
						<ul class="user_info">
							<li class="single_field">
								<label>国家:</label>
								<select>
									<option>United States</option>
									<option>Bangladesh</option>
									<option>UK</option>
									<option>India</option>
									<option>Pakistan</option>
									<option>Ucrane</option>
									<option>Canada</option>
									<option>Dubai</option>
								</select>
								
							</li>
							<li class="single_field">
								<label>区域/状态：</label>
								<select>
									<option>Select</option>
									<option>Dhaka</option>
									<option>London</option>
									<option>Dillih</option>
									<option>Lahore</option>
									<option>Alaska</option>
									<option>Canada</option>
									<option>Dubai</option>
								</select>
							
							</li>
							<li class="single_field zip-field">
								<label>邮编：</label>
								<input type="text">
							</li>
						</ul>
						<a class="btn btn-default update" href="">获得报价</a>
						<a class="btn btn-default check_out" href="">继续</a>
					</div>
				</div>
				<div class="col-sm-6">
					<div class="total_area">
						<ul>
							<li>总金额 <span id="subtotal">￥{{$subtotal}}</span></li>
							<li>生态税 <span id="tax">-</span></li>
							<li>航运成本 <span>Free</span></li>
							<li>总共 <span id="subtotals">￥{{$subtotal}} </span></li>
						</ul>
							<a class="btn btn-default update" href="{{route('destory-cart')}}">清空购物车</a>
							<a class="btn btn-default check_out" href="{{ route('checkout') }}">结算</a>
					</div>
				</div>
			</div>
		</div>
	</section><!--/#do_action-->

	
	
	
	@include('front.layouts.footer')
	@include('front.layouts.script-src')
	<script type="text/javascript">

		function clearboth()
		{
			$.get('{{route('set-clear-both')}}');
		}
		

		function update_count(id,rowId,type,qty=null,stocks)
		{
			var stock = $("#stock").text().substr(3);
			// alert(stocks);

			if($("#quantity-"+id).val() > stock){

				alert('不能大于库存');
				$("#quantity-"+id).val(stock);
				return false;

			}
			var qty = parseInt($("#quantity-"+id).val());

			$.post('/shop-crat/'+rowId+'/update',{'_token':"{{csrf_token()}}",'qty':$("#quantity-"+id).val(),'oldqty':qty,'type':type},function(rsp){

				if(rsp.status === 'plus'){

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#quantity-"+id).val('('+qty+')'); 
					$("#quantity-"+id).val(qty+=1); 
					$("#tax").text('￥ '+rsp.tax);
					$('#tl-'+id).text('￥ '+$("#quantity-"+id).val() * $('#price-'+id).text());
					// $("#subtotal").text('￥ '+$())

				}else if(rsp.status === 'ajan'){

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#quantity-"+id).val('('+qty+')'); 
					$("#quantity-"+id).val(qty-=1); 
					$("#tax").text('￥ '+rsp.tax);
					$('#tl-'+id).text('￥ '+$("#quantity-"+id).val() * $('#price-'+id).text());
				}else if(rsp.status === 'maxqtx'){
					alert('超过库存(不要作恶)');
				}
				else{

					$("#subtotal").text('￥ '+rsp.subtotal);
					$("#subtotals").text('￥ '+rsp.subtotal);
					$("#countCart").text(rsp.count);
					$("#tax").text('￥ '+rsp.tax);
					$("#quantity-"+id).val(qty); 
					$('#tl-'+id).text('￥ '+ $("#quantity-"+id).val() * $('#price-'+id).text());


				}

				if(rsp.status === 'null'){
					window.location.href='';
				}
			})
		}
 
	</script>
@endsection
	
